package cn.iocoder.yudao.module.crossborder.order.controller.admin.ordercustomer;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.crossborder.order.controller.admin.ordercustomer.vo.*;
import cn.iocoder.yudao.module.crossborder.order.dal.dataobject.ordercustomer.OrderCustomerDO;
import cn.iocoder.yudao.module.crossborder.order.service.ordercustomer.OrderCustomerService;

@Tag(name = "管理后台 - 跨境电商客户信息")
@RestController
@RequestMapping("/crossborder/order-customer")
@Validated
public class OrderCustomerController {

    @Resource
    private OrderCustomerService orderCustomerService;

    @PostMapping("/create")
    @Operation(summary = "创建跨境电商客户信息")
    @PreAuthorize("@ss.hasPermission('crossborder:order-customer:create')")
    public CommonResult<Long> createOrderCustomer(@Valid @RequestBody OrderCustomerSaveReqVO createReqVO) {
        return success(orderCustomerService.createOrderCustomer(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新跨境电商客户信息")
    @PreAuthorize("@ss.hasPermission('crossborder:order-customer:update')")
    public CommonResult<Boolean> updateOrderCustomer(@Valid @RequestBody OrderCustomerSaveReqVO updateReqVO) {
        orderCustomerService.updateOrderCustomer(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除跨境电商客户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crossborder:order-customer:delete')")
    public CommonResult<Boolean> deleteOrderCustomer(@RequestParam("id") Long id) {
        orderCustomerService.deleteOrderCustomer(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除跨境电商客户信息")
                @PreAuthorize("@ss.hasPermission('crossborder:order-customer:delete')")
    public CommonResult<Boolean> deleteOrderCustomerList(@RequestParam("ids") List<Long> ids) {
        orderCustomerService.deleteOrderCustomerListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得跨境电商客户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:order-customer:query')")
    public CommonResult<OrderCustomerRespVO> getOrderCustomer(@RequestParam("id") Long id) {
        OrderCustomerDO orderCustomer = orderCustomerService.getOrderCustomer(id);
        return success(BeanUtils.toBean(orderCustomer, OrderCustomerRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得跨境电商客户信息分页")
    @PreAuthorize("@ss.hasPermission('crossborder:order-customer:query')")
    public CommonResult<PageResult<OrderCustomerRespVO>> getOrderCustomerPage(@Valid OrderCustomerPageReqVO pageReqVO) {
        PageResult<OrderCustomerDO> pageResult = orderCustomerService.getOrderCustomerPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderCustomerRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出跨境电商客户信息 Excel")
    @PreAuthorize("@ss.hasPermission('crossborder:order-customer:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderCustomerExcel(@Valid OrderCustomerPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderCustomerDO> list = orderCustomerService.getOrderCustomerPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "跨境电商客户信息.xls", "数据", OrderCustomerRespVO.class,
                        BeanUtils.toBean(list, OrderCustomerRespVO.class));
    }

}